package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.TxJl
import client.comm.baoding.databinding.LayoutHomeTjhwBinding
import client.comm.baoding.databinding.LayoutTdnewitemBinding
import client.comm.baoding.databinding.LayoutTjhwBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.ui.GoodsDetailTradeActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.dipToPx
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.google.gson.Gson

/**
 * 精选商品
 *
 */
class HomeTjGoodAdapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<GoodsIndex.TjGoods>(object : DiffUtil.ItemCallback<GoodsIndex.TjGoods>() {
        val g = Gson()

        override fun areItemsTheSame(
            oldItem: GoodsIndex.TjGoods,
            newItem: GoodsIndex.TjGoods
        ): Boolean {
            return true
        }

        override fun areContentsTheSame(
            oldItem: GoodsIndex.TjGoods,
            newItem: GoodsIndex.TjGoods
        ): Boolean {
            return g.toJson(oldItem).equals(g.toJson(newItem))
        }
    }) {

    //设置商品label标签
    private fun getLabel(str: String): View? {
        val child = TextView(context)
        child.textSize = 12f
        var drawId = when (str) {
            "热卖" -> R.drawable.label_bg_red1_0
            "新品" -> R.drawable.label_bg_green1_0
            "爱心商品" -> R.drawable.label_bg_blue1_0
            else -> null
        }
        if (drawId != null) {
            child.background = context.getDrawable(drawId)
        }

        child.setTextColor(context.resources.getColor(R.color.white))
        child.setText(str)
        child.setPadding(dipToPx(context.resources, 2f).toInt())
        // 定义LayoutParam
        val params: RelativeLayout.LayoutParams =
            RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.rightMargin = dipToPx(context.resources, 10f).toInt()
        child.layoutParams = params
        // 调用带有LayoutParams参数的addView方法
        return child//mContainer.addView(child, params)
    }

    fun itemClick(bean: GoodsIndex.TjGoods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
        val binding = holder.binding as LayoutHomeTjhwBinding
        val bean = t as GoodsIndex.TjGoods
        binding.bean = bean
        binding.event = this

        manager.load(bean.goods_img)
            .into(binding.img)

        TextTool.getBuilder("¥").setAlign(Layout.Alignment.ALIGN_CENTER).setBold().setTextSize(14)
            .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837")).setBold().setTextSize(16)
            .into(binding.tvPrice)

        binding.llLabel.removeAllViews()
        for (e in bean.goods_label) {
            binding.llLabel.addView(getLabel(e))
        }
    }

    override fun getItemLayout(): Int {
        return R.layout.layout_home_tjhw
    }

}


